<template>
  <div>
    <div v-if="value.predicateName=='Between'">
      <a-range-picker
        style="width: 100%"
        show-time
        :placeholder="[i18n('predicate.startTime','开始时间'), i18n('predicate.endTime','结束时间')]"
        @change="onChange"
        :value="args"
      />
    </div>
    <div v-else>
      <a-date-picker style="width: 100%" :value="args" show-time :placeholder="i18n('predicate.selectTime','选择时间')" @change="onChange"/>
    </div>
  </div>
</template>

<script>
  import moment from "moment";

  export default {
    name: "DateEdit",
    props: ["value"],
    data() {
      return {
        args: ""
      }
    },
    created() {
      this.init();
    },
    methods: {
      i18n(name,text,key){
          return this.$ti18(name,text,"SysRouting",key);
      },
      init() {
        if (this.value.predicateArgs) {
          if (this.value.predicateName == "Between") {
            this.args = [];
            var predicateArgsStart = JSON.parse(this.value.predicateArgs)._genkey_0;
            var predicateArgsEnd = JSON.parse(this.value.predicateArgs)._genkey_1;
            var startDate = predicateArgsStart.split("T");
            var startTime = (startDate[1].split("+"))[0];
            var startDateTime = startDate[0] + " " + startTime;
            this.args.push(moment(startDateTime, "YYYY-MM-DD hh:dd:ss"));
            var endDate = predicateArgsEnd.split("T");
            var endTime = (endDate[1].split("+"))[0];
            var endDateTime = endDate[0] + " " + endTime;
            this.args.push(moment(endDateTime, "YYYY-MM-DD hh:dd:ss"));
          } else {
            var date = (JSON.parse(this.value.predicateArgs))._genkey_0.split("T");
            var time = (date[1].split("+"))[0];
            var dateTime = date[0] + " " + time;
            this.args = moment(dateTime, "YYYY-MM-DD hh:dd:ss");
          }
        } else {
          this.args = "";
        }
      },
      onChange(date, dateString) {
        if (date) {
          if (this.value.predicateName == 'Between') {
            var startDateStr = dateString[0].replace(" ", "T") + ".000+08:00[Asia/Shanghai]";
            var endDateStr = dateString[1].replace(" ", "T") + ".000+08:00[Asia/Shanghai]";
            var obj = {
              _genkey_0: startDateStr,
              _genkey_1: endDateStr
            };
            this.value.predicateArgs = JSON.stringify(obj);
          } else {
            var dateStr = dateString.replace(" ", "T") + ".000+08:00[Asia/Shanghai]";
            var obj = {
              _genkey_0: dateStr
            }
            this.value.predicateArgs = JSON.stringify(obj);
          }
        }
        this.args = date;
      },
    },
  }
</script>

<style scoped>

</style>